#include "DCHAR.h"
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#ifdef WIN32
#include <windows.h>
#include "platform/third_party/win32/iconv/iconv.h"
#endif //WIN32

void dsprintfA(char * szBuffer, int size, const char * szFormat, ...)
{
	va_list pArgList;
	va_start(pArgList, szFormat);
	vsnprintf(szBuffer, size, szFormat, pArgList);
	va_end(pArgList);
}
void dsprintfW(wchar_t * szBuffer, int size, const wchar_t * szFormat, ...)
{
	va_list pArgList;
	va_start(pArgList, szFormat);
	vswprintf(szBuffer, size, szFormat, pArgList);
	va_end(pArgList);
}
void dsprintfArgA(char * szBuffer, int size, const char * szFormat, va_list pArgList)
{
	vsnprintf(szBuffer, size, szFormat, pArgList);
}
void dsprintfArgW(wchar_t * szBuffer, int size, const wchar_t * szFormat, va_list pArgList)
{
	vswprintf(szBuffer, size, szFormat, pArgList);
}
float dstrtofA(const char * str)
{
	return (float)atof(str);
}
float dstrtofW(const wchar_t * str)
{
	return (float)wcstod(str, NULL);
}
long long dstrtoi64A(const char * str, int base)
{
	if (base == 10)
	{
		if (str[0] == '-')
		{
			return _atoi64(str);
		}
		else
		{
			return _strtoui64(str, NULL, 10);
		}
	}
	else if (base == 16)
	{
		return _strtoui64(str, NULL, 16);
	}
	return 0;
}
long long dstrtoi64W(const wchar_t * str, int base)
{
	if (base == 10)
	{
		return _wtoi64(str);
	}
	else if (base == 16)
	{
		return _wcstoui64(str, NULL, 16);
	}
	return 0;
}
STRING dUnicode2MultiByte(const wchar_t * str)
{
	char * p = new char[(wcslen(str) + 1) * 2];
	memset(p, 0, (wcslen(str) + 1) * 2);
	int len = WideCharToMultiByte(CP_ACP, 0, str, (int)wcslen(str), p, (int)(wcslen(str) + 1) * 2, NULL, NULL);
	std::string r = p;
	delete[] p;
	return r;
}
WSTRING dMultiByte2Unicode(const char * str)
{
	wchar_t * str1;
	str1 = new wchar_t(strlen(str) + 1);
	MultiByteToWideChar(CP_ACP, 0, str, (int)strlen(str) + 1, str1, (int)strlen(str) + 1);
	std::wstring r = str1;
	delete[] str1;
	return r;
}